package com.ophiux.wechat.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ophiux.wechat.mapper.OphiuxAppUserAnswerMapper;
import com.ophiux.wechat.mapper.OphiuxProblemAnswerMapper;
import com.ophiux.wechat.mapper.OphiuxProblemMapper;
import com.ophiux.wechat.model.dto.OphiuxProblemAggregationDTO;
import com.ophiux.wechat.model.dto.ProblemAnswerAgreementDTO;
import com.ophiux.wechat.model.entity.OphiuxAppUserAnswer;
import com.ophiux.wechat.model.entity.OphiuxProblemOptions;
import com.ophiux.wechat.model.query.OphiuxProblemAnswerVo;
import com.ophiux.wechat.service.OphiuxProblemOptionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @desc:
 * @author: hhl
 * @date：2023/10/13 11:16
 */
@Controller
@RequestMapping("/ophiuxProblemAnswer")
@Api(tags = "查看答案")
public class OphiuxProblemAnswerController {

    @Autowired
    OphiuxAppUserAnswerMapper ophiuxAppUserAnswerMapper;
    @Autowired
    OphiuxProblemAnswerMapper ophiuxProblemAnswerMapper;
    @Autowired
    OphiuxProblemOptionsService ophiuxProblemOptionsService;
    @Autowired
    OphiuxProblemMapper ophiuxProblemMapper;


    @GetMapping
    @ApiOperation("查询答案列表")
    public JSONArray get(@RequestParam(required = false, defaultValue = "1") Integer current,
                         @RequestParam(required = false, defaultValue = "50") Integer size, OphiuxProblemAnswerVo vo) {
        JSONArray array = new JSONArray();
        List<OphiuxAppUserAnswer> paramList = ophiuxAppUserAnswerMapper.getParamList(vo);
        for (OphiuxAppUserAnswer user : paramList) {
            JSONObject respJSON = new JSONObject();
            respJSON.put("患者id", user.getPatientId());
            respJSON.put("答题记录id", user.getId());
            respJSON.put("答题类型", user.getUseType());
            List<ProblemAnswerAgreementDTO> answerList = ophiuxProblemAnswerMapper.getListByAnswerRecordId(user.getId());
            for (ProblemAnswerAgreementDTO answer : answerList) {
                if (answer.getProblemType() == 3) {
                    respJSON.put(answer.getProblemTitle(), answer.getAnswer());
                } else {
                    List<String> ids = Arrays.stream(answer.getAnswerIds().split(",")).collect(Collectors.toList());
                    List<OphiuxProblemOptions> optionsList = ophiuxProblemOptionsService.list(new LambdaQueryWrapper<OphiuxProblemOptions>().in(OphiuxProblemOptions::getId, ids));
                    String optionsName = optionsList.stream().map(OphiuxProblemOptions::getOptionName).collect(Collectors.joining(","));
                    respJSON.put(answer.getProblemTitle(), optionsName);
                }
            }
            array.add(respJSON);
        }
        return array;
    }



    @RequestMapping("/freemarkerIndex")
    public String index(Map<String, Object> result, OphiuxProblemAnswerVo vo) {
        JSONArray array = new JSONArray();
        Page<OphiuxAppUserAnswer> paramList1 = ophiuxAppUserAnswerMapper.getParamList(new Page(vo.getCurrent(), vo.getSize()), vo);
        List<OphiuxAppUserAnswer> paramList = paramList1.getRecords();
        for (OphiuxAppUserAnswer user : paramList) {
            JSONObject respJSON = new JSONObject();
            respJSON.put("patientId", user.getPatientId());
            respJSON.put("answerRecordId", user.getId());
            respJSON.put("useType", user.getUseType());
            respJSON.put("createdTime",user.getCreatedTime());

            List<ProblemAnswerAgreementDTO> answerList = ophiuxProblemAnswerMapper.getListByAnswerRecordId(user.getId());
            JSONArray problemArray = new JSONArray();
            for (ProblemAnswerAgreementDTO answer : answerList) {
                if (answer == null) continue;
                JSONObject problem = new JSONObject();
                problem.put("problemTitle", answer.getProblemTitle());
                if (answer.getProblemType() == 3) {
                    problem.put("problemAnswer", answer.getAnswer());
                } else {
                    if (!ObjectUtils.isEmpty(answer.getAnswerIds())) {
                        List<String> ids = Arrays.stream(answer.getAnswerIds().split(",")).collect(Collectors.toList());
                        List<OphiuxProblemOptions> optionsList = ophiuxProblemOptionsService.list(new LambdaQueryWrapper<OphiuxProblemOptions>().in(OphiuxProblemOptions::getId, ids));
                        String optionsName = optionsList.stream().map(OphiuxProblemOptions::getOptionName).collect(Collectors.joining(","));
                        problem.put("problemAnswer", optionsName);
                    }else{
                        problem.put("problemAnswer","未答");
                    }
                }
                problemArray.add(problem);
            }
            respJSON.put("problemList", problemArray);
            array.add(respJSON);
        }

        int start = (vo.getCurrent() - 1) * 2;
        int end = start + vo.getSize();
        BigDecimal totalPage = new BigDecimal(paramList1.getTotal()).divide(new BigDecimal(vo.getSize()), RoundingMode.UP);
        result.put("totalPage", totalPage.intValue());
        result.put("current", vo.getCurrent());
        result.put("size", vo.getSize());
        result.put("useType",vo.getUseType());


        result.put("list", array);
        result.put("total",array.size());
        return "index";
    }



    @RequestMapping("/freemarkerIndex2")
    public String index2(Map<String, Object> result, OphiuxProblemAnswerVo vo) {
        JSONArray array = new JSONArray();
        Page<OphiuxAppUserAnswer> paramList1 = ophiuxAppUserAnswerMapper.getParamList(new Page(vo.getCurrent(), vo.getSize()), vo);
        List<OphiuxAppUserAnswer> paramList = paramList1.getRecords();
        for (OphiuxAppUserAnswer user : paramList) {
            JSONObject respJSON = new JSONObject();
            respJSON.put("patientId", user.getPatientId());
            respJSON.put("answerRecordId", user.getId());
            respJSON.put("useType", user.getUseType());
            respJSON.put("createdTime",user.getCreatedTime());

            List<ProblemAnswerAgreementDTO> answerList = ophiuxProblemAnswerMapper.getListByAnswerRecordId(user.getId());
            JSONArray problemArray = new JSONArray();
            for (ProblemAnswerAgreementDTO answer : answerList) {
                if (answer == null) continue;
                JSONObject problem = new JSONObject();
                problem.put("problemTitle", answer.getProblemTitle());
                if (answer.getProblemType() == 3) {
                    problem.put("problemAnswer", answer.getAnswer());
                } else {
                    if (!ObjectUtils.isEmpty(answer.getAnswerIds())) {
                        List<String> ids = Arrays.stream(answer.getAnswerIds().split(",")).collect(Collectors.toList());
                        List<OphiuxProblemOptions> optionsList = ophiuxProblemOptionsService.list(new LambdaQueryWrapper<OphiuxProblemOptions>().in(OphiuxProblemOptions::getId, ids));
                        String optionsName = optionsList.stream().map(OphiuxProblemOptions::getOptionName).collect(Collectors.joining(","));
                        problem.put("problemAnswer", optionsName);
                    }else{
                        problem.put("problemAnswer","未答");
                    }
                }
                problemArray.add(problem);
            }
            respJSON.put("problemList", problemArray);
            array.add(respJSON);
        }

        int start = (vo.getCurrent() - 1) * 2;
        int end = start + vo.getSize();
        BigDecimal totalPage = new BigDecimal(paramList1.getTotal()).divide(new BigDecimal(vo.getSize()), RoundingMode.UP);
        result.put("totalPage", totalPage.intValue());
        result.put("current", vo.getCurrent());
        result.put("size", vo.getSize());
        result.put("useType",vo.getUseType());

        result.put("list", array);
        result.put("total",array.size());
        return "index2";
    }


    @RequestMapping("/freemarkerIndex3")
    public String index3(Map<String, Object> result, OphiuxProblemAnswerVo vo) {
        JSONArray array = new JSONArray();
        Page<OphiuxAppUserAnswer> paramList1 = ophiuxAppUserAnswerMapper.getParamList(new Page(vo.getCurrent(), vo.getSize()), vo);

        List<OphiuxProblemAggregationDTO> problemList = ophiuxProblemMapper.getProblemList(vo.getUseType());
        result.put("problemList", problemList);

        List<OphiuxAppUserAnswer> paramList = paramList1.getRecords();
        for (OphiuxAppUserAnswer user : paramList) {
            JSONObject respJSON = new JSONObject();
            respJSON.put("patientId", user.getPatientId());
            respJSON.put("answerRecordId", user.getId());
            respJSON.put("useType", user.getUseType());
            respJSON.put("createdTime",user.getCreatedTime());

            List<ProblemAnswerAgreementDTO> answerList = ophiuxProblemAnswerMapper.getListByAnswerRecordId(user.getId());
            List<String> answerList2 = new ArrayList<>();
            for (ProblemAnswerAgreementDTO answer : answerList) {
                if (answer == null) continue;
                if (answer.getProblemType() == 3) {
                    answerList2.add(answer.getAnswer());
                } else {
                    if (!ObjectUtils.isEmpty(answer.getAnswerIds())) {
                        List<String> ids = Arrays.stream(answer.getAnswerIds().split(",")).collect(Collectors.toList());
                        List<OphiuxProblemOptions> optionsList = ophiuxProblemOptionsService.list(new LambdaQueryWrapper<OphiuxProblemOptions>().in(OphiuxProblemOptions::getId, ids));
                        String optionsName = optionsList.stream().map(OphiuxProblemOptions::getOptionName).collect(Collectors.joining(","));
                        answerList2.add(optionsName);
                    }else{
                        answerList2.add("");
                    }
                }
            }
            respJSON.put("answerList", answerList2);
            array.add(respJSON);
        }

        int start = (vo.getCurrent() - 1) * 2;
        int end = start + vo.getSize();
        BigDecimal totalPage = new BigDecimal(paramList1.getTotal()).divide(new BigDecimal(vo.getSize()), RoundingMode.UP);
        result.put("totalPage", totalPage.intValue());
        result.put("current", vo.getCurrent());
        result.put("size", vo.getSize());
        result.put("useType",vo.getUseType());

        result.put("list", array);
        return "index3";
    }

}
